package com.ml.mall.api.company;

import com.ml.mall.annotation.LogMethodInvokeInformation;
import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.req.company.UCChangeCompanyDto;
import com.ml.mall.entity.company.SysUserCompanyRelation;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.service.company.SysUserCompanyRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @description 用户公司关联信息接口
 * @author yx
 * @date 2021/10/11
 * @version 1.0
*/
@Api(tags = "【auth】用户公司关联信息接口")
@RestController
@RequestMapping(path = "/user_company")
public class SysUCRelationController {
    @Autowired
    private SysUserCompanyRelationService userCompanyRelationService;

    @ApiOperation("获取当前用户的公司关联信息")
    @PostMapping(path = "/query_self")
    @LogMethodInvokeInformation("获取当前用户的公司关联信息")
    public ApiRestResult selfUCRelationQuery(){
        List<SysUserCompanyRelation> result = userCompanyRelationService.selfUCRelationQuery();
        if(result == null || result.size() == 0){
            return ApiRestResult.err(CommonEnum.SELECT_ERROR);
        }
        return ApiRestResult.ok(result);
    }

    @ApiOperation("切换当前账号对应的公司")
    @PostMapping(path = "/change_company")
    @LogMethodInvokeInformation("切换当前账号对应的公司")
    public ApiRestResult changeCompany(@RequestBody @Valid UCChangeCompanyDto dto){
        boolean result = userCompanyRelationService.changeCompany(dto);
        if(!result){
            ApiRestResult.err(CommonEnum.UPDATE_ERROR);
        }
        return ApiRestResult.ok(result);
    }
}
